package com.sgl.gk.service.impl;

import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.utils.StringUtils;
import com.sgl.gk.domain.GkSignalCommunication;
import com.sgl.gk.mapper.GkSignalCommunicationMapper;
import com.sgl.gk.service.GkSignalCommunicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GkSignalCommunicationServiceImpl implements GkSignalCommunicationService{

    @Autowired
    GkSignalCommunicationMapper gkSignalCommunicationMapper;

    @Override
    public List<GkSignalCommunication> list(GkSignalCommunication gkSignalCommunication) {
        return gkSignalCommunicationMapper.list(gkSignalCommunication);
    }

    @Override
    public AjaxResult add(GkSignalCommunication gkSignalCommunication) {
        gkSignalCommunication.setCreateAt(new Date());
        // todo 是否需要对通讯设备状态进行状态判断
        gkSignalCommunication.setType(0);
        gkSignalCommunication.setPort(StringUtils.trimToNull(gkSignalCommunication.getPort()));
        gkSignalCommunication.setIp(StringUtils.trimToNull(gkSignalCommunication.getIp()));
        if (StringUtils.length(gkSignalCommunication.getPort())>10) {
            return AjaxResult.error("端口号长度不能超过10个字符");
        }
        if (StringUtils.length(gkSignalCommunication.getCommunicationProtocol())>20){
            return AjaxResult.error("通信协议不能超过20个字符");
        }
        if (StringUtils.length(gkSignalCommunication.getName())>200) {
            return AjaxResult.error("通信名称不能超过100个字符");
        }
        if (StringUtils.length(gkSignalCommunication.getIp())>20){
            return AjaxResult.error("IP不能超过25个字符");
        }
        int row = gkSignalCommunicationMapper.add(gkSignalCommunication);
        if (row>0){
            return AjaxResult.success("插入成功");
        }
        return AjaxResult.error("插入失败");
    }

    @Override
    public AjaxResult edit(GkSignalCommunication gkSignalCommunication) {
        if (StringUtils.length(gkSignalCommunication.getPort())>10) {
            return AjaxResult.error("端口号长度不能超过10个字符");
        }
        if (StringUtils.length(gkSignalCommunication.getCommunicationProtocol())>20){
            return AjaxResult.error("通信协议不能超过20个字符");
        }
        if (StringUtils.length(gkSignalCommunication.getName())>200) {
            return AjaxResult.error("通信名称不能超过100个字符");
        }
        if (StringUtils.length(gkSignalCommunication.getIp())>20){
            return AjaxResult.error("IP不能超过25个字符");
        }
        int row = gkSignalCommunicationMapper.edit(gkSignalCommunication);
        if (row>0){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    @Override
    public AjaxResult removeByIds(Long[] ids) {
        int row = gkSignalCommunicationMapper.removeByIds(ids);
        if (row>0){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }
}
